
package Frontera;

import Control.Sandpile;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class MainWindow extends JFrame {
    
    /*Especificación de las variables que determinan el tamano de la interfaz
    y las velocidades de ejecución de las reglas.*/  
    
    private final int ROWCELLS = 50;
    private final int MWIDTH = 800;
    private final int MHEIGHT = 700;
    private final int SIZE = MHEIGHT / ROWCELLS;
    private final int NORM = 200;
    private final int SLOW = 800;
    private final int FAST = 50;
    private Sandpile sandpile;
    

    public MainWindow() {
        intialize();
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    
    //Inicializa la inerfaz y le otorga sus características.
    private void intialize() {
        sandpile = new Sandpile(0, 0, ROWCELLS - 1, ROWCELLS - 1, ROWCELLS);
        setLayout(new BorderLayout(2, 2));
        
        this.setSize(MWIDTH, MHEIGHT);
        this.setResizable(false);
        this.setTitle("CSandpile");
        this.addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setSize(100, 800);
        
        
        final JLabel Top = new JLabel("    Top:");
        buttonPanel.add(Top);
        
        
        final JTextField tope = new JTextField();
        tope.setColumns(5);
        
        
        buttonPanel.add(tope);
        
        
        final JButton clearButton = new JButton(new ImageIcon("images/clear-icon.png"));
        clearButton.setSize(32, 32);
        clearButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent ae) {
                sandpile.clear();
            }
        });
        
        final JPanel cellPanel = new JPanel();
        
        cellPanel.setLayout(
        new GridLayout(ROWCELLS, ROWCELLS));
        cellPanel.setSize(HEIGHT, HEIGHT);
        
        
        ImageIcon startIcon = new ImageIcon("images/Start.png");
        JButton startButton = new JButton(startIcon);
        startButton.setSize(32, 32);
        
        
        
        startButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent ae) {
                clearButton.setEnabled(false);
                Thread thread = new Thread(sandpile);
                thread.start();
                
                
                cellPanel.removeAll();        
                   
                for (int i = 0;i < ROWCELLS;i++) {
                    for (int j = 0; j < ROWCELLS; j++) {
                        ACView cell = new ACView(SIZE, Integer.parseInt(tope.getText()));
                        cell.setAC(sandpile.getAC(i, j));
                        sandpile.getAC(i, j).setCell(cell);
                        cellPanel.add(cell);
                    }
                }
                cellPanel.updateUI();
                cellPanel.repaint();
            }
        });
        
        buttonPanel.add(startButton);
        JButton stopButton = new JButton(new ImageIcon("images/stop.png"));
        stopButton.setSize(32, 32);
        stopButton.addActionListener(
                new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        sandpile.stop();
                        clearButton.setEnabled(true);
                    }
                });
        
        buttonPanel.add(stopButton);       
        buttonPanel.add(clearButton);
        

        
        String[] speeds = {"Numbers", "Colors", "Other"};
        
        final JComboBox speedBox = new JComboBox(speeds);        
        sandpile.setSpeed(FAST);
        
        speedBox.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent ae) {
                int index = speedBox.getSelectedIndex();
                if (index == 0) {
                    sandpile.setSpeed(FAST);
                } else if (index == 1) {
                    sandpile.setSpeed(NORM);
                } else if (index == 2) {
                    sandpile.setSpeed(SLOW);
                }
            }
        });
        
        buttonPanel.add(speedBox);
        
       
        
        this.add(buttonPanel, BorderLayout.PAGE_START);
        for (int i = 0;i < ROWCELLS;i++) {
            for (int j = 0; j < ROWCELLS; j++) {
                ACView cell = new ACView(SIZE,4);
                cell.setAC(sandpile.getAC(i, j));
                sandpile.getAC(i, j).setCell(cell);
                cellPanel.add(cell);
            }
        }
                
        this.add(cellPanel);
    }
}
